<template>
  <van-cell-group class="panel-raise-create">
    <van-field required label-width="70px" label="申请单位" input-align="right" readonly placeholder="请选择申请单位" v-model="applyDept" @click="showPageApplyDeptList = true" is-link/>
    <van-field required label-width="70px" label="考核单位" input-align="right" readonly placeholder="请选择考核单位" v-model="checkDept" @click="startSelectAssessDept" is-link/>
    <van-field required label-width="70px" label="考核载体" input-align="right" readonly placeholder="请选择考核载体" v-model="carrierText" @click="startSelectCarrierItem" is-link/>
    <van-field required label-width="70px" label="考核内容" input-align="right" readonly placeholder="请选择考核内容" v-model="contentText" @click="startSelectContentItem" is-link/>
    <van-row v-if="carrier.content_item.id !== ''">
      <van-row class="first-row">{{carrier.content_item.item.substr(carrier.content_item.item.indexOf('。') + 1)}}</van-row>
      <van-row class="split-h last-row"/>
    </van-row>
    <van-field required label-width="70px" label="考核事项" input-align="right" readonly placeholder="点击选择考核事项" @click="startSelectScoreItem" is-link/>
    <van-row v-if="carrier.content_item.score_item.length > 0">
      <van-row
        v-for="(score) in carrier.content_item.score_item"
        :key="score.id"
        class="first-row">
        {{score.id + ' - ' + score.item}}
      </van-row>
      <van-row class="split-h last-row"/>
    </van-row>
    <van-field required label-width="70px" label="加分理由" v-model="raiseReason" rows="1" autosize type="textarea" maxlength="200" placeholder="请输入申请加分理由" show-word-limit/>
    <van-field required label-width="70px" label="加分分值" input-align="right">
      <template #input>
        <van-stepper theme="round" v-model="score" :decimal-length="2" min="0.00" step="0.01" input-width="70%" button-size="22"/>
      </template>
    </van-field>
    <van-field required label-width="200px" label="添加附件 - ( 5M以内 )"/>
    <van-row style="padding: 10px 16px 2px;">
      <van-uploader result-type='file' @oversize="onOversize" v-model="fileList" accept="*" :max-count="1" :max-size="5 * 1024 * 1024" upload-icon="plus" preview-size="60"/>
    </van-row>
    <van-row class="split-h last-row"/>
    <card-receivers v-if="apply" :parentPath="applyDept" v-model="receivers"/>
    <van-row class="split-h last-row"/>
    <van-row class='operate-area' gutter="10">
      <van-col span="12">
        <van-button type="info" @click="submit('1')">结束加分申请</van-button>
      </van-col>
      <van-col span="12">
        <van-button type="info" @click="submit('0')">继续加分申请</van-button>
      </van-col>
    </van-row>
    <van-popup
      v-model="showPageApplyDeptList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-apply-dept-list v-if="showPageApplyDeptList" v-on:onCancel="showPageApplyDeptList = false" v-on:onConfirm="doSelectedApplyDept"/>
    </van-popup>
    <van-popup
      v-model="showPageAssessDeptList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-assess-dept-list v-if="showPageAssessDeptList" :assessType="assessType" :depart="assessDept" v-on:onCancel="showPageAssessDeptList = false" v-on:onConfirm="doSelectedAssessDept"/>
    </van-popup>
    <van-popup
      v-model="showPageCarrierItemList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-carrier-item-list v-if="showPageCarrierItemList" :carrier="carrier" :type="$vo.CarrierType.ADD" v-on:onCancel="showPageCarrierItemList = false" v-on:onConfirm="endSelectCarrierItem"/>
    </van-popup>
    <van-popup
      v-model="showPageContentItemList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-content-item-list v-if="showPageContentItemList" :carrier="carrier" :type="$vo.CarrierType.ADD" v-on:onCancel="showPageContentItemList = false" v-on:onConfirm="endSelectContentItem"/>
    </van-popup>
    <van-popup
      v-model="showPageScoreItemList"
      position="right"
      get-container="body"
      style="width: 100%; height: 100%; background-color: #F9F9F9">
      <page-sore-item-list v-if="showPageScoreItemList" :carrier="carrier" :type="$vo.CarrierType.ADD" v-on:onCancel="showPageScoreItemList = false" v-on:onConfirm="endSelectScoreItem"/>
    </van-popup>
    <loading-overlay :show="isLoading"/>
  </van-cell-group>
</template>

<script>
import PageAssessDeptList from '@/views/common/page/PageAssessDeptList'
import PageApplyDeptList from '@/views/common/page/PageApplyDeptList'
import PageCarrierItemList from '@/views/common/page/PageCarrierItemList'
import PageContentItemList from '@/views/common/page/PageContentItemList'
import PageSoreItemList from '@/views/common/page/PageSoreItemList'
import CardReceivers from '@/views/common/card/CardReceivers'
import { uploadFile } from '@/api/upload'
import { updateApplyInst } from '@/api/assess'
export default {
  name: 'PanelRaiseModify',
  components: { PageAssessDeptList, PageApplyDeptList, PageCarrierItemList, PageContentItemList, PageSoreItemList, CardReceivers },
  props: ['assessType', 'apply'],
  data () {
    return {
      isLoading: false,
      showPageAssessDeptList: false,
      showPageApplyDeptList: false,
      showPageCarrierItemList: false,
      showPageContentItemList: false,
      showPageScoreItemList: false,
      applyDept: this.apply.apply_inst.apply_dept,
      checkDept: this.apply.apply_inst.asse_dept,
      carrier: JSON.parse(this.apply.apply_inst.carrier_item),
      raiseReason: this.apply.apply_inst.add_reson,
      score: this.apply.apply_inst.score,
      fileList: [],
      receivers: []
    }
  },
  methods: {
    onOversize () {
      this.$dialog.alert({
        message: '所选附件超过限定大小，请重新选择！'
      })
    },
    doSelectedApplyDept (dept) {
      this.applyDept = dept
      this.checkDept = ''
      this.showPageApplyDeptList = false
    },
    doSelectedAssessDept (dept) {
      this.checkDept = dept
      this.showPageAssessDeptList = false
    },
    startSelectAssessDept () {
      if (this.applyDept.trim() === '') {
        this.$dialog.alert({
          message: '请先选择申请部门'
        })
      } else {
        this.showPageAssessDeptList = true
      }
    },
    startSelectCarrierItem () {
      this.showPageCarrierItemList = true
    },
    endSelectCarrierItem (carrier) {
      this.carrier = carrier
      this.showPageCarrierItemList = false
    },
    startSelectContentItem () {
      if (this.carrier.id === '') {
        this.$dialog.alert({
          message: '请先选择考核载体'
        })
      } else {
        this.showPageContentItemList = true
      }
    },
    endSelectContentItem (content) {
      this.carrier.content_item = content
      this.showPageContentItemList = false
    },
    startSelectScoreItem () {
      console.log('this.carrier.content_item.id', this.carrier.content_item.id)
      if (this.carrier.content_item.id === '') {
        this.$dialog.alert({
          message: '请先选择考核内容'
        })
      } else {
        this.showPageScoreItemList = true
      }
    },
    endSelectScoreItem (score) {
      this.carrier.content_item.score_item = score
      console.log('score', this.carrier.content_item.score_item)
      this.showPageScoreItemList = false
    },
    submit (flag) {
      if (flag === '0') {
        if (this.applyDept.trim() === '') {
          this.$dialog.alert({
            message: '请选择申请部门'
          })
          return
        }
        if (this.checkDept.trim() === '') {
          this.$dialog.alert({
            message: '请选择考核部门'
          })
          return
        }
        if (this.carrierText.trim() === '') {
          this.$dialog.alert({
            message: '请选择考核载体'
          })
          return
        }
        if (this.contentText.trim() === '') {
          this.$dialog.alert({
            message: '请选择考核内容'
          })
          return
        }
        if (this.carrier.content_item.score_item.length === 0) {
          this.$dialog.alert({
            message: '请选择考核事项'
          })
          return
        }
        if (this.raiseReason.trim() === '') {
          this.$dialog.alert({
            message: '请输入加分理由'
          })
          return
        }
        if (this.score === 0) {
          this.$dialog.alert({
            message: '请输入加分分值'
          })
          return
        }
        // if (!this.fileList[0]) {
        //   this.$dialog.alert({
        //     message: '请上传附件材料'
        //   })
        //   return
        // }
        if (this.receivers.length === 0) {
          this.$dialog.alert({
            message: '请选择接收人员'
          })
          return
        }
        this.$dialog.confirm({
          message: '确认提交加分申请?'
        }).then(() => {
          this.doCreateApply()
        }).catch(() => {
        })
      } else {
        this.$dialog.confirm({
          message: '确认结束加分申请?'
        }).then(() => {
          this.doCloseApply()
        }).catch(() => {
        })
      }
    },
    doCloseApply () {
      this.isLoading = true
      let applyObj = {
        op: this.$vo.OperateType.MODIFY,
        opcontent: this.$vo.createOperate(),
        apply_inst: this.apply.apply_inst
      }
      applyObj.opcontent.approve_userid = this.$store.state.authInfo.user.userid
      applyObj.opcontent.approve_username = this.$store.state.authInfo.user.username
      applyObj.opcontent.approve_result = '1'
      applyObj.opcontent.problem_id = this.apply.apply_inst.id
      this.doUpdateApply(applyObj)
    },
    doCreateApply () {
      this.isLoading = true

      let applyObj = {
        op: this.$vo.OperateType.APPLY,
        opcontent: this.$vo.createOperate(),
        apply_inst: this.$vo.createApply()
      }

      applyObj.apply_inst.id = this.apply.apply_inst.id
      applyObj.apply_inst.apply_dept = this.applyDept
      applyObj.apply_inst.asse_dept = this.checkDept
      applyObj.apply_inst.carrier_item = JSON.stringify(this.carrier)
      applyObj.apply_inst.add_reson = this.raiseReason
      applyObj.apply_inst.score = this.score
      applyObj.apply_inst.user_id = this.$store.state.authInfo.user.userid
      applyObj.apply_inst.user_name = this.$store.state.authInfo.user.username
      applyObj.apply_inst.apply_type = this.assessType
      applyObj.opcontent.approve_userid = this.$store.state.authInfo.user.userid
      applyObj.opcontent.approve_username = this.$store.state.authInfo.user.username
      applyObj.opcontent.approve_result = '0'
      applyObj.opcontent.problem_id = this.apply.apply_inst.id
      applyObj.opcontent.receivers = this.receivers
      applyObj.apply_inst.file_url = ''
      applyObj.apply_inst.file_name = ''

      if (this.fileList.length === 0) {
        this.doUpdateApply(applyObj)
      } else {
        let fileData = new FormData()
        fileData.append('file', this.fileList[0].file)
        uploadFile(fileData).then(res => {
          let { data } = res
          if (data[this.$response.CODE] === this.$response.SUCCESS) {
            let file = data[this.$response.DATA]
            applyObj.apply_inst.file_url = file.file_url
            applyObj.apply_inst.file_name = file.file_name
            this.doUpdateApply(applyObj)
          } else {
            this.isLoading = false
            this.$toast.fail('上传附件失败：' + data[this.$response.MESSAGE])
          }
        }, errFunc => {
          this.isLoading = false
          this.$notify('服务调用失败：' + errFunc.toString())
          console.log(errFunc.toString())
        })
      }
    },
    doUpdateApply (applyObj) {
      updateApplyInst(applyObj).then(res => {
        this.isLoading = false
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          this.$toast.success('加分申请数据提交成功')
          this.$emit('onConfirm')
        } else {
          this.$toast.fail('加分申请数据提交失败：' + data[this.$response.MESSAGE])
        }
      }, errFunc => {
        this.isLoading = false
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    }
  },
  computed: {
    carrierText: function () {
      if (this.carrier.id !== '') {
        return this.carrier.id + ' - ' + this.carrier.item
      } else {
        return ''
      }
    },
    contentText: function () {
      if (this.carrier.content_item.id !== '') {
        return this.carrier.content_item.id + ' - ' + this.carrier.content_item.item.substr(0, this.carrier.content_item.item.indexOf('。'))
      } else {
        return ''
      }
    },
    applyText: function () {
      let str = ''
      switch (this.apply.apply_inst.apply_status) {
        case 0:
          str = '待审核'
          break
        case 1:
          str = '待复核'
          break
        case 2:
          str = '已完成'
          break
      }
      return str
    },
    assessDept: function () {
      if (this.applyDept.trim() !== '') {
        let tmp = this.applyDept.split('/')
        return tmp[0] + '/' + tmp[1]
      } else {
        return ''
      }
    }
  }
}
</script>

<style scoped lang="less">
  .panel-raise-create {
    .van-field {
      font-size: 12px;
    }
    .first-row {
      font-size: 12px;
      color: #969799;
      padding: 5px 16px;
    }
    .last-row {
      margin-left: 16px;
    }
    .operate-area {
      padding: 20px 16px;
      .van-button {
        width: 100%;
      }
    }
  }
</style>
